import levin.guiCommon as guiCommon
#import levin.wineAreas as wineAreas
from levin.world.bottle import Bottle
import levin.wineAreasWidget as wineAreasWidget


import levin.mainWindow 
import levin.mainScreen
import levin.settingScreen

import levin.wineEdit
import levin.tastingEdit
import levin.tastingList as tastingList

appComponents  = guiCommon.appComponents

def initAppComponents():
    wineWorld = appComponents.wineWorld
    
    # main window
    topWindow = levin.mainWindow.TopWindow()
    #mainScreen = levin.mainWindow.MainScreen(name='mainScreen')
    mainScreen = levin.mainScreen.MainScreenDouble(name='mainScreen')
    topWindow.add_widget(mainScreen)

    settingScreen = levin.settingScreen.SettingScreen(name='settings')
    topWindow.add_widget(settingScreen)
    print ' VVV ',mainScreen._button_bar
    mainScreen._mainListContainer.add_widget(appComponents.wineWidgetTree)
    mainScreen._mainListContainer.add_widget(guiCommon.visibleWinesLabel)

    # -----------------------------------
    # next, instantiate components and bind them.
    # -----------------------------------
    import levin.wineDisplay as wineDisplay
    wDisplay = wineDisplay.WineDisplay()
    mainScreen._sidepanel.bottleTab.add_tab(_('Description'),wDisplay)

    import levin.tastingDisplay as tastingDisplay
    tDisplay = tastingDisplay.TastingDisplay()
    mainScreen._sidepanel.bottleTab.add_tab(_('Tastings'),tDisplay)
    tDisplay.tastingList.setDebugPrefix('tDisplay')

    from levin.sidePanel import AreaDisplay
    aDisplay = AreaDisplay()
    mainScreen._sidepanel.areaTab.add_tab(_('Map'), aDisplay)


    appComponents.topWindow  = topWindow
    appComponents.mainScreen = mainScreen
    appComponents.tastingDisplay = tDisplay
    appComponents.wineDisplay = wDisplay
    appComponents.areaDisplay = aDisplay
    appComponents.tastingListWidget = tastingList.tastingListWidget

    

    def wineNodeSelected(o,node):
        mainScreen._sidepanel.selectedObject = node
        if isinstance(node,Bottle):
            guiCommon.commonEvents.selectedBottle = node


    appComponents.wineWidgetTree.bind(_selected_node=wineNodeSelected)

    def tastingNodeSelected(o,node):
        t = tastingList.tastingListWidget._selected_node.tasting
        b = t.bottle
        mainScreen._sidepanel.selectedObject = b
        guiCommon.commonEvents.selectedBottle = b
        appComponents.tastingListWidget.selectedTasting = t # this allows the tasting display system 
                                                            # to know which tasting widget is selected
        if tDisplay.bottle:
            ## tastingList.last_selected = t
            i = b.tastings.index(t)
            ## ## tDisplay.tastingList.none_selectable = False ||| old code using MyListView
            ## ## tDisplay.tastingList.select_node_at(i)
            ## ## tDisplay.tastingList.none_selectable = True
            adapter = tDisplay.tastingList.adapter
            if adapter.cached_views != {} : # then the tasting widgets already exist
                # reset their is_selected property :
                for ind, wid in adapter.cached_views.iteritems():
                    wid.is_selected = ( t is wid.tasting )
            ## print ' uuuu ',i, adapter.cached_views
            ## adapter.select_list( [ adapter.cached_views[i] ] )
            pass
        
    def switch_to_tastinglist_mode(*l):
        if tastingList.tastingListWidget is None:
            print ' iii Building ...'
            tlw = tastingList.TastingList()
            tastingList.tastingListWidget  = tlw
            appComponents.tastingListWidget = tlw                        
            print ' iii Populating ...'
            tlw.populate()
            tlw.bind(_selected_node = tastingNodeSelected)
            tDisplay.tastingList.adapter.args_converter = tastingDisplay.args_converter_withTastingList

        tastingList.tastingListWidget.doVisibilityUpdate = True
        from levin.wineSelectorWidgets import NumSelectorWid
        NumSelectorWid.mainInstance.defaultSelection = 'incellarornomore'
        NumSelectorWid.mainInstance.reset()
        appComponents.wineWorld.updateSelector()
        tastingList.tastingListWidget.header.sortParentTree(0, reverse=True) # sort on dates
        mainScreen._mainListContainer.clear_widgets()
        print ' iii add widgets ...'
        mainScreen._mainListContainer.add_widget(tastingList.tastingListWidget)
        mainScreen._mainListContainer.add_widget(guiCommon.visibleWinesLabel)
        print ' iii done'

    def switch_to_cellar_mode(*l):
        tastingList.tastingListWidget.doVisibilityUpdate = False

        mainScreen._mainListContainer.clear_widgets()
        from levin.wineSelectorWidgets import NumSelectorWid
        NumSelectorWid.mainInstance.defaultSelection = 'incellar'
        NumSelectorWid.mainInstance.reset()
        appComponents.wineWorld.updateSelector()
        mainScreen._mainListContainer.add_widget(appComponents.wineWidgetTree)
        mainScreen._mainListContainer.add_widget(guiCommon.visibleWinesLabel)
        
    guiCommon.commonEvents.bind( on_cellar_mode = switch_to_cellar_mode, on_tastinglist_mode = switch_to_tastinglist_mode)
